<?php
Header("content-type: application/x-javascript");
require_once('includes/inc.php');
checkLang($_GET['j']);

	$dn="d";//$_GET['dn'];
	switch($dn)
	{
		case 'd':
			$back="back";
			$sback="sback";
			$fcolor="#000080";
			$jwunit="#4dadfb";
			$sitem="#d5ebfd";
			$loader="ajaxd";
			break;
		case 'n':
			$back="backn";
			$sback="sbackn";
			$fcolor="#111319";
			$jwunit="#3f4447";
			$sitem="#c0c3c6";
			$loader="ajaxn";
			break;
		default:
			break;
	}
	
?>
/* 
* Menuvanje na kodna tastatura od latinicna
*  vo Kirilicna Windows-1251
*/

C=new Array(
  'А','Б','В','Г','Д','Е','З','Ѕ','И','Ј','К','Л','Љ','М','Н','Њ','О',
  'П','Р','С','Т','У','Ф','Х','Ц','Џ',
  'а','б','в','г','д','е','з','ѕ','и','ј','к','л','љ','м','н','њ','о',
  'п','р','с','т','у','ф','х','ц','џ')

L=new Array(
  'A','B','V','G','D','E','Z','Y','I','J','K','L','Q','M','N','W','O',
  'P','R','S','T','U','F','H','C','X',
  'a','b','v','g','d','e','z','y','i','j','k','l','q','m','n','w','o',
  'p','r','s','t','u','f','h','c','x')

//------------------------------------
Cyr=new Array()
	
	for (q=0;q<59;q++) {
	//  if (q==27 || q==57) continue
	  Cyr[L[q]]=C[q]
	
}

Lat=new Array()
	
	for (q=0;q<59;q++) {
	//  if (q==27 || q==57) continue
	  Lat[C[q]]=L[q]
	
}

Cyr[unescape('%5C')]=('Ѓ') 
Cyr[unescape('%7C')]=('ѓ')   
Cyr[unescape('%40')]=('Ж')
Cyr[unescape('%60')]=('ж')
Cyr[unescape('%7D')]=('ќ')
Cyr[unescape('%5D')]=('Ќ')
Cyr[('^')]=('Ч') 
Cyr[unescape('%7E')]=('ч')
Cyr[unescape('%5B')]=('Ш')
Cyr[unescape('%7B')]=('ш')
//Cyr[unescape('%62')]=unescape('%E1')

/*
* Funkcoja za proverka dali stringot e kirilica
*/

function isCyr(elem){
	var alphaExp = /^[а-шА-Ш]+$/;
	if(elem.match(alphaExp)){
		return true;
	}else{
		return false;
	}
}

/* 
* Funkcija za konverzija na string od latinica vo kirilica
*/
function toCyr(lat) {
  cyr=''
  for (q=0;q<lat.length;q++) {
    ch=lat.charAt(q)
    chL=ch.toLowerCase()
    nxt=lat.charAt(q+1).toLowerCase()


   
 if (Cyr[ch]) cyr+=Cyr[ch]
    else cyr+=ch
  }
  return cyr
}

/* 
* Funkcija za konverzija na string od kirilica vo latinica
*/
function toLat(lat) {
  cyr=''
  for (q=0;q<lat.length;q++) {
    ch=lat.charAt(q)
    chL=ch.toLowerCase()
    nxt=lat.charAt(q+1).toLowerCase()


   
 if (Lat[ch]) cyr+=Lat[ch]
    else cyr+=ch
  }
  return cyr
}

/* 
* KRAJ NA FUNKCII ZA KONVERZIJA - LATINICA - KIRILICA
*/







/* 
* funkcija sto ja menuva 
* vrednosta na serachboxot spored suggest
*/
function smeni(item){
	jwlocation.value=item;
	getWeather('X','c');
	document.getElementById("jwsuggestion").style.display="none";
	jwlocation.value="";
}








/* 
* proverka dali e pritisat Enter ili keyup keydown
*/
var elemid=0;
var idxgrad="";
function enter(e)
{
	vald=jwlocation.value;
	if(e.keyCode==13) 
	{
		if(idxgrad.length>0) getWeather(idxgrad,'c');
		else getWeather('X','c');
		jwlocation.value="";
		idxgrad="";
	}
	if(e.keyCode==38) pomesti('up');
	else if(e.keyCode==40) pomesti('down');
	else elemid=0;
}









/* 
* funkcija za iteriranje niz sugegstion lista so pomos na strelki
*/

function pomesti(dir){
	ee=document.getElementsByName("suggitem");
	
	if(dir=="down") 
	{
		if(elemid==ee.length) { elemid=1; idx=0; }
		else idx=elemid++;
		
	}
	else if(dir=="up") 
	{
		if(idx==0) {elemid=1; idx=0;}
		else idx=--elemid-1;
	}
	for(i=0;i<ee.length;i++)
	{
		ee[i].className="suggitem";
	}
	ee[idx].className="sughover";
	idxgrad=ee[idx].id;
}











// pomosni promenlivi
var letnum=0;
var fletnum=0;


/* funkcija za predlog na gradovi od baza*/
function suggest(e)
{
	if(e.keyCode!=13 && e.keyCode!=38 && e.keyCode!=40)
	{
	string=jwlocation.value;
	string=toLat(string);
	if(string.length==0) 
	{
		document.getElementById("jwsuggestion").style.display="none";
	}
	else if(string.length>1)
	{	
		document.getElementById("jwsuggestion").style.display="block";
		
		xmlhttpS=GetXmlHttpObject();
		if (xmlhttpS==null)
		{
			alert ("Your browser does not support XML HTTP Request");
			return;
		}
		len=string.length;
		var url="suggest.php";
		url=url+"?str="+string;
		url=url+"&len="+len;
		xmlhttpS.onreadystatechange=stateChangedd;
		xmlhttpS.open("GET",url,true);
		xmlhttpS.send(null);
		return false;
	}
	}
}

function stateChangedd()
  {
  if (xmlhttpS.readyState==4)
    {
		str=xmlhttpS.responseText;
		if(str.length>0)
    document.getElementById("jwsuggestion").innerHTML=xmlhttpS.responseText;
	else document.getElementById("jwsuggestion").style.display="none";
    }
  }
  

  
  
 /* 
* ajax funkcija sto ke generira Google Maps - mapa spored longitutde i latitude
*/
  function getMap(longitude,latitude,temp,cond,grad)
{
	document.getElementById("map").innerHTML='<img src=img/<?php echo $loader;?>.gif>'
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
    {
		alert ("Your browser does not support XML HTTP Request");
		return;
    }
	var url="getMap.php";
	url=url+"?grad="+grad;
	url=url+"&temp="+temp;
	url=url+"&cond="+cond;
	url=url+"&long="+longitude;
	url=url+"&lat="+latitude;
	xmlhttp.onreadystatechange=stateChangeddd;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
  return false;
  }

function stateChangeddd()
  {
  if (xmlhttp.readyState==4)
    {
		document.getElementById("map").innerHTML=xmlhttp.responseText;
    }
  }
  
  
  
  
  
var uu='c';
  
/* 
* ajax funkcija sto ke go zema vremeto za dadena lokacija spored parametrite
*/
function getWeather(grad,unit)
  {	
	if(grad=="X") grad=jwlocation.value;
	grad=toLat(grad);
	jwlocation.value="";

	
  document.getElementById("jwwrapper").innerHTML='<img src=img/<?php echo $loader;?>.gif>'
  xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
    {
    alert ("Your browser does not support XML HTTP Request");
    return;
    }
	var url="getInfo.php";
  url=url+"?grad="+grad;
  url=url+"&unit="+uu;
  xmlhttp.onreadystatechange=stateChanged;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  return false;
  }

function stateChanged()
  {
  if (xmlhttp.readyState==4)
    {
    document.getElementById("jwwrapper").innerHTML=xmlhttp.responseText;
	document.getElementById("jwsuggestion").style.display="none";
	if(uu=='f') toF();
    }
  }

  
  
  
  
  
  
  
  
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
} 






function convert(unit)
{
	switch(unit){
		case 'F': 
		case 'f':
				toF();
				break;
		case 'C': 
		case 'c':
				toC();
				break;
		default:
			break;
	}
}

function prikaziGraf()
{
	document.getElementById("loading").style.display='block';
	if(!paramsCheck()) 
	{
		alert('<?php echo _MISS; ?>');
		document.getElementById("loading").style.display='none';
		return false;
	}
	document.getElementById("grafici").innerHTML="";
	url="?datum=2009-12-05|2009-12-25&gr=Skopje|Struga|Berovo|Bitola|Stip|Veles|Debar&gt=t";
	url="";
	datum="?datum=";
	gr="&gr=";
	
	denodd=denod.value;
	dendoo=dendo.value;
	
	mesecodd=mesecod.value;
	mesecdoo=mesecdo.value;
	
	gododd=godod.value;
	goddoo=goddo.value;
	
	
	datum+=gododd+"-"+mesecodd+"-"+denodd+"|"+goddoo+"-"+mesecdoo+"-"+dendoo;
	
	
	for(i=0;i<gradovi.length;i++)
	{
		if(gradovi[i].selected) gr+=(gradovi[i].value)+"|";
	}
	gr=gr.substr(0,gr.length-1);
	
	url+=datum+gr+"&gt=";
	
	if(params.param[0].checked) genGraph0(url);
	if(params.param[1].checked) genGraph1(url);
	if(params.param[2].checked) genGraph2(url);
	if(params.param[3].checked) genGraph3(url);
	if(params.param[4].checked) genGraph4(url);
	/*for(i=0;i<params.param.length;i++)
	{
		url1=url;
		if(params.param[i].checked)
		{
			url1+=params.param[i].value;
			genGraph(url1);	
		}
		
		
	}*/
}

// graf 1 - temperatura
function genGraph0(u)
  {	
  u+='t';
  xmlhttp0=GetXmlHttpObject();
	if (xmlhttp0==null)
    {
    alert ("Your browser does not support XML HTTP Request");
    return;
    }
	var url="grafik.php";
  url=url+u;
  xmlhttp0.onreadystatechange=stateChangedg0;
  xmlhttp0.open("GET",url,true);
  xmlhttp0.send(null);
  return false;
  }

function stateChangedg0()
  {
  if (xmlhttp0.readyState==4)
    {
	if(errCheck(xmlhttp0.responseText))
	{
		document.getElementById("grafici").innerHTML+='<img src='+xmlhttp0.responseText+'/>';
	}
	else
	{
		document.getElementById("grafici").innerHTML+='<?php echo _NOINFO; ?>';
	}
	document.getElementById("loading").style.display='none';
    }
  }
  
  // graf 2 - vlaznost
  function genGraph1(u)
  {	
  u+='h';
  xmlhttp1=GetXmlHttpObject();
	if (xmlhttp1==null)
    {
    alert ("Your browser does not support XML HTTP Request");
    return;
    }
	var url="grafik.php";
  url=url+u;
  xmlhttp1.onreadystatechange=stateChangedg1;
  xmlhttp1.open("GET",url,true);
  xmlhttp1.send(null);
  return false;
  }

function stateChangedg1()
  {
  if (xmlhttp1.readyState==4)
    {
    if(errCheck(xmlhttp1.responseText))
	{
		document.getElementById("grafici").innerHTML+='<img src='+xmlhttp1.responseText+'/>';
	}
	else
	{
		document.getElementById("grafici").innerHTML+='<?php echo _NOINFO; ?>';
	}
	document.getElementById("loading").style.display='none';
    }
  }
  
    // graf 3 - vidlivost
  function genGraph2(u)
  {	
  u+='v';
  xmlhttp2=GetXmlHttpObject();
	if (xmlhttp2==null)
    {
    alert ("Your browser does not support XML HTTP Request");
    return;
    }
	var url="grafik.php";
  url=url+u;
  xmlhttp2.onreadystatechange=stateChangedg2;
  xmlhttp2.open("GET",url,true);
  xmlhttp2.send(null);
  return false;
  }

function stateChangedg2()
  {
  if (xmlhttp2.readyState==4)
    {
    if(errCheck(xmlhttp2.responseText))
	{
		document.getElementById("grafici").innerHTML+='<img src='+xmlhttp2.responseText+'/>';
	}
	else
	{
		document.getElementById("grafici").innerHTML+='<?php echo _NOINFO; ?>';
	}
	document.getElementById("loading").style.display='none';
    }
  }
  
   // graf 4 - pritisok
 function genGraph3(u)
  {	
  u+='p';
  xmlhttp3=GetXmlHttpObject();
	if (xmlhttp3==null)
    {
    alert ("Your browser does not support XML HTTP Request");
    return;
    }
	var url="grafik.php";
  url=url+u;
  xmlhttp3.onreadystatechange=stateChangedg3;
  xmlhttp3.open("GET",url,true);
  xmlhttp3.send(null);
  return false;
  }

function stateChangedg3()
  {
  if (xmlhttp3.readyState==4)
    {
   	if(errCheck(xmlhttp3.responseText))
	{
		document.getElementById("grafici").innerHTML+='<img src='+xmlhttp3.responseText+'/>';
	}
	else
	{
		document.getElementById("grafici").innerHTML+='<?php echo _NOINFO; ?>';
	}
	document.getElementById("loading").style.display='none';
    }
  }
  
     // graf 5 - veter
 function genGraph4(u)
  {	
  u+='w';
  xmlhttp4=GetXmlHttpObject();
	if (xmlhttp4==null)
    {
    alert ("Your browser does not support XML HTTP Request");
    return;
    }
	var url="grafik.php";
  url=url+u;
  xmlhttp4.onreadystatechange=stateChangedg4;
  xmlhttp4.open("GET",url,true);
  xmlhttp4.send(null);
  return false;
  }

function stateChangedg4()
  {
  if (xmlhttp4.readyState==4)
    {
    if(errCheck(xmlhttp4.responseText))
	{
		document.getElementById("grafici").innerHTML+='<img src='+xmlhttp4.responseText+'/>';
	}
	else
	{
		document.getElementById("grafici").innerHTML+='<?php echo  _NOINFO; ?>';
	}
	document.getElementById("loading").style.display='none';
    }
  }

  
  
 // proverka dali rezultatot sodrzi greska 
function errCheck(str)
{
	if(str.indexOf("err")!=-1) return false;
	else return true;
}

// proverka dali se izbrani site potrebni parametri 
function paramsCheck()
{
	okg=false;
	okp=false;
	for(i=0;i<gradovi.length;i++)
	{
		if(gradovi[i].selected) okg=true;
	}
	for(i=0;i<params.param.length;i++)
	{
		if(params.param[i].checked) okp=true;
	}
	
	if(okg==false || okp==false) return false;
	else return true;
}
function toF()
{
	uu='f';
	// temperatura
	val=document.getElementById("jwtemp").innerHTML;
	val=val.substr(0,val.length-3);
	val=val*9/5+32;
	val=val.toFixed(0);
	val+=" °F";
	document.getElementById("jwtemp").innerHTML=val;
	
	//pritisok
	val=document.getElementById("jwpritisok").innerHTML;
	val=val.substr(0,val.length-3);
	val=val*0.0295301;
	val=val.toFixed(2);
	val+=" in";
	document.getElementById("jwpritisok").innerHTML=val;
	
	//vidlivost
	val=document.getElementById("jwvid").innerHTML;
	val=val.substr(0,val.length-3);
	val=val*0.621371192;
	val=val.toFixed(2);
	val+=" Mi";
	document.getElementById("jwvid").innerHTML=val;
	
	//veter
	val=document.getElementById("jwwind").innerHTML;
	val=val.substr(0,val.length-4);
	val=val*0.621371192;
	val=val.toFixed(2);
	val+=" mph";
	document.getElementById("jwwind").innerHTML=val;
	
	document.getElementById("jwunit").innerHTML='<a href="#" title="<?php echo _KONVERTIRAJC; ?>" onclick="javascript:convert(\'C\');">°C</a> |';
}

function toC()
{
	uu='c';
	// temperatura
	val=document.getElementById("jwtemp").innerHTML;
	val=val.substr(0,val.length-3);
	val=(val - 32) * 5/9;
	val=val.toFixed(0);
	val+=" °C";
	document.getElementById("jwtemp").innerHTML=val;
	
	//pritisok
	val=document.getElementById("jwpritisok").innerHTML;
	val=val.substr(0,val.length-3);
	val=val*33.8637526;
	val=val.toFixed(2);
	val+=" mb";
	document.getElementById("jwpritisok").innerHTML=val;
	
	//vidlivost
	val=document.getElementById("jwvid").innerHTML;
	val=val.substr(0,val.length-3);
	val=val*1.609344;
	val=val.toFixed(1);
	val+=" Km";
	document.getElementById("jwvid").innerHTML=val;
	
	//veter
	val=document.getElementById("jwwind").innerHTML;
	val=val.substr(0,val.length-4);
	val=val*1.609344;
	val=val.toFixed(2);
	val+=" kph";
	document.getElementById("jwwind").innerHTML=val;
	
	document.getElementById("jwunit").innerHTML='<a href="#" title="<?php echo _KONVERTIRAJF; ?>" onclick="javascript:convert(\'F\');">°F</a> |'
}